import cv2 as cv
import numpy as np
import os

recognizer = cv.face.LBPHFaceRecognizer_create()
recognizer.read('trainner.yml')

img = cv.imread('C:/Users/Administrator/Desktop/opencv/2.jpg')#加载
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)#灰度图
face_detector = cv.CascadeClassifier('C:/Users/Administrator/Desktop/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml')# 创建级联分类器
faces = face_detector.detectMultiScale(gray,1.1,5)# 对图像进行检测
for x,y,w,h in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
for x,y,w,h in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
    id,confidence=recognizer.predict(gray[y:y+h,x:x+w])
    print(id ,' ',confidence)


cv.imshow('result',img)
cv.waitKey(0)
cv.destroyAllWindows() #必备
